package demo15;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {
        // 创建一个Map对象，这里使用HashMap实现
        Map<String, String> books = new HashMap<>();

        // 添加元素到Map
        books.put("1984", "George Orwell");
        books.put("To Kill a Mockingbird", "Harper Lee");
        books.put("The Great Gatsby", "F. Scott Fitzgerald");

        // 打印Map中的所有元素
        System.out.println("Initial Map: " + books);

        // 访问Map中的元素
        String authorOf1984 = books.get("1984");
        System.out.println("Author of '1984': " + authorOf1984);

        // 修改Map中的元素
        books.put("To Kill a Mockingbird", "Harper Lee (Revised Edition)");
        System.out.println("Map after modification: " + books);

        // 删除Map中的元素
        books.remove("The Great Gatsby");
        System.out.println("Map after removal: " + books);

        // 检查Map是否包含某个键
        boolean containsKey = books.containsKey("1984");
        System.out.println("Does the map contain the key '1984'? " + containsKey);

        // 检查Map是否包含某个值
        boolean containsValue = books.containsValue("Harper Lee (Revised Edition)");
        System.out.println("Does the map contain the value 'Harper Lee (Revised Edition)'? " + containsValue);

        // 获取Map的大小
        int size = books.size();
        System.out.println("Size of the map: " + size);

        // 遍历Map中的元素
        System.out.println("Iterating through the map:");
        for (Map.Entry<String, String> entry : books.entrySet()) {
            System.out.println("Book: " + entry.getKey() + ", Author: " + entry.getValue());
        }
    }
}
